﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:Drafter.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:Drafter.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/

namespace Drafter.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources:
    /// </para>
    /// <code>
    /// &lt;Application.Resources&gt;
    ///     &lt;vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:Drafter.ViewModel"
    ///                                  x:Key="Locator" /&gt;
    /// &lt;/Application.Resources&gt;
    /// </code>
    /// <para>
    /// Then use:
    /// </para>
    /// <code>
    /// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    /// </code>
    /// <para>
    /// You can also use Blend to do all this with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// <para>
    /// In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete
    /// the Main property and bind to the ViewModelNameStatic property instead:
    /// </para>
    /// <code>
    /// xmlns:vm="clr-namespace:Drafter.ViewModel"
    /// DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
    /// </code>
    /// </summary>
    public class ViewModelLocator
    {
       /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            //TODO: Cleanup
            //ClearInitial();
            //ClearMainWindow();
            //ClearEdit();  
            //ClearOptions();


        }
        #region Initial
        
        private static InitialViewModel _initial;

        /// <summary>
        /// Gets the Initial property.
        /// </summary>
        public static InitialViewModel InitialStatic
        {
            get
            {
                if (_initial == null)
                {
                    CreateInitial();
                }

                return _initial;
            }
        }

        /// <summary>
        /// Gets the Initial property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public InitialViewModel Initial
        {
            get
            {
                return InitialStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Initial property.
        /// </summary>
        public static void ClearInitial()
        {
            _initial.Cleanup();
            _initial = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Initial property.
        /// </summary>
        public static void CreateInitial()
        {
            if (_initial == null)
            {
                _initial = new InitialViewModel();
            }
        }

        #endregion

        #region main
        private static MainWindowViewModel _mainWindow;

        /// <summary>
        /// Gets the MainWindow property.
        /// </summary>
        public static MainWindowViewModel MainWindowStatic
        {
            get
            {
                if (_mainWindow == null)
                {
                    CreateMainWindow();
                }

                return _mainWindow;
            }
        }

        /// <summary>
        /// Gets the MainWindow property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainWindowViewModel MainWindow
        {
            get
            {
                return MainWindowStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the MainWindow property.
        /// </summary>
        public static void ClearMainWindow()
        {
            _mainWindow.Cleanup();
            _mainWindow = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the MainWindow property.
        /// </summary>
        public static void CreateMainWindow()
        {
            if (_mainWindow == null)
            {
                _mainWindow = new MainWindowViewModel();
            }
        }


        #endregion

        #region edit
        private static EditViewModel _viewModelPropertyName;

        /// <summary>
        /// Gets the Edit property.
        /// </summary>
        public static EditViewModel EditStatic
        {
            get
            {
                if (_viewModelPropertyName == null)
                {
                    CreateEdit();
                }

                return _viewModelPropertyName;
            }
        }

        /// <summary>
        /// Gets the Edit property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public EditViewModel Edit
        {
            get
            {
                return EditStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Edit property.
        /// </summary>
        public static void ClearEdit()
        {
            _viewModelPropertyName.Cleanup();
            _viewModelPropertyName = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Edit property.
        /// </summary>
        public static void CreateEdit()
        {
            if (_viewModelPropertyName == null)
            {
                _viewModelPropertyName = new EditViewModel();
            }
        } 
        #endregion

        #region options
        private static OptionsViewModel _options;

        /// <summary>
        /// Gets the Options property.
        /// </summary>
        public static OptionsViewModel OptionsStatic
        {
            get
            {
                if (_options == null)
                {
                    CreateOptions();
                }

                return _options;
            }
        }

        /// <summary>
        /// Gets the Options property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public OptionsViewModel Options
        {
            get
            {
                return OptionsStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Options property.
        /// </summary>
        public static void ClearOptions()
        {
            _options.Cleanup();
            _options = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Options property.
        /// </summary>
        public static void CreateOptions()
        {
            if (_options == null)
            {
                _options = new OptionsViewModel();
            }
        } 
        #endregion

        #region about
        private static AboutViewModel _about;

        /// <summary>
        /// Gets the Options property.
        /// </summary>
        public static AboutViewModel AboutStatic
        {
            get
            {
                if (_about == null)
                {
                    CreateAbout();
                }

                return _about;
            }
        }

        /// <summary>
        /// Gets the Options property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public AboutViewModel About
        {
            get
            {
                return AboutStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Options property.
        /// </summary>
        public static void ClearAbout()
        {
            _about.Cleanup();
            _about = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Options property.
        /// </summary>
        public static void CreateAbout()
        {
            if (_about == null)
            {
                _about = new AboutViewModel();
            }
        }
        #endregion
    }
}